package com.lanchewuyou.user.driver.lipeiui;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

import com.afollestad.materialdialogs.MaterialDialog;
import com.lanchewuyou.user.driver.R;
import com.lanchewuyou.user.driver.modul.Config;
import com.lanchewuyou.user.driver.modul.OkHttpClientManager;
import com.rengwuxian.materialedittext.MaterialEditText;
import com.squareup.okhttp.Request;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * 到达目的地后提交最终信息界面
 */

public class PickCarOverActivity extends AppCompatActivity {
    private Toolbar mToolbar;
    private Spinner spinner;
    private MaterialEditText fixPhone;
    private Button pickCarOverBtn;
    private ArrayAdapter adapter;
    private List<String> list;
    private String yuyueTime = "暂无";
    private ProgressDialog progressDialog;
    private static String DRIVERID;

    public static PickCarOverActivity instance;
    private Toast toast1 = null;

    private void showTextToast(String msg) {
        if (toast1 == null) {
            toast1 = Toast.makeText(this, msg, Toast.LENGTH_SHORT);
        } else {
            toast1.setText(msg);
        }
        toast1.show();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pick_car_over);
        initViews();
        instance = this;
        SharedPreferences sharedPreferences = getSharedPreferences(Config.PASSWORD_PATH, Activity.MODE_PRIVATE);
        DRIVERID = sharedPreferences.getString("driverid", "");
    }

    private void initViews() {
        mToolbar = (Toolbar) findViewById(R.id.pick_car_over_toolbar);
        mToolbar.setTitle("信息确认");
        setSupportActionBar(mToolbar);
        getSupportActionBar().setHomeButtonEnabled(true);
        spinner = (Spinner) findViewById(R.id.pick_car_over_spinner);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        list = new ArrayList<>();
        list.add("暂定");
        for (int i = 0; i < 10; i++) {
            Calendar cal = Calendar.getInstance();// 取当前日期。
            cal.add(Calendar.DAY_OF_MONTH, +i);
            String data = format.format(cal.getTime());
            list.add(data);
        }
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
        //第三步：为适配器设置下拉列表下拉时的菜单样式。
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                parent.setVisibility(View.VISIBLE);
                yuyueTime = list.get(position);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                parent.setVisibility(View.VISIBLE);
            }
        });
        fixPhone = (MaterialEditText) findViewById(R.id.pick_over_fix_phone);
        pickCarOverBtn = (Button) findViewById(R.id.pick_car_over_next_btn);
        pickCarOverBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                progressDialog.show();
                OkHttpClientManager.getAsyn(Config.RETURNCAROVER + PickCarActivity.orderId + "&driverid=" + DRIVERID + "&time=" + yuyueTime + "&contact=" + fixPhone.getText().toString(),
                        new OkHttpClientManager.StringCallback() {
                            @Override
                            public void onFailure(Request request, IOException e) {
                                progressDialog.dismiss();
                                showTextToast("上传失败");
                            }

                            @Override
                            public void onResponse(String response) {
                                try {
                                    JSONObject jsonObject = new JSONObject(response);
                                    String status = jsonObject.getString("status");
                                    if (status.equals("1")) {
                                        finish();
                                        progressDialog.dismiss();
                                    } else {
                                        progressDialog.dismiss();
                                        showTextToast("请求失败");
                                    }
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }

                            }
                        });
            }
        });
        progressDialog = new ProgressDialog(PickCarOverActivity.this);
        progressDialog.setMessage("请稍等");
        progressDialog.setTitle("信息上传中");
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setCancelable(true);
    }

    @Override
    public void onBackPressed() {
        showBackDialog();
    }

    private void showBackDialog() {
        new MaterialDialog.Builder(this)
                .title("退出任务")
                .content("点击确定将退出您正在执行的任务。")
                .positiveText("确定")
                .negativeText("取消")
                .callback(new MaterialDialog.ButtonCallback() {
                    @Override
                    public void onPositive(MaterialDialog dialog) {
                        finish();
                    }

                    @Override
                    public void onNegative(MaterialDialog dialog) {
                        super.onNegative(dialog);
                    }
                })
                .show();
    }
}
